﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using OSVDB_Model;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace OSVDB_Logic
{
    public class SecFoc_BL : IVulns
    {
        private String url;

        public SecFoc_BL(String url)
        {
            this.url = url;
        }

        #region IVulns Members

        public Collection<OSVDB_Model.AbstractVuln> getAll()
        {
            Collection<OSVDB_Model.AbstractVuln> list = new Collection<OSVDB_Model.AbstractVuln>();
            WebClient wc = new WebClient();
            string xml = wc.DownloadString(url);

            XDocument doc = XDocument.Parse(xml);

            var items = from item in doc.Descendants("item")
                        //orderby item.Element("pubDate").Value descending
                        select item;

            foreach (XElement element in items)
            {
                try
                {
                    list.Add(new OSVDB_Model.SecFoc(element.Element("title").Value.ToString(), element.Element("link").Value.ToString(), element.Element("description").Value.ToString(), getFormattedDate(element.Element("pubDate").Value.ToString())));
                }
                catch (Exception)
                {

                }
             }

            return list;
        }

        private DateTime getFormattedDate(string p)
        {
            if(p == "")
            {
                p = DateTime.Now.ToShortDateString();
            }
           DateTime date = DateTime.Parse(p);
           return date;
        }

        public OSVDB_Model.AbstractVuln getSpecific()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
